﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using VIBlend.WPF.Controls;

namespace AnonsaiWPF.CSharp.DataGridViewDemos
{
    [Category("VIBlend.WPF.Examples")]
    public partial class MaskEditorDemo: Window, IWindow
    {
        #region IWindow Members

        public void DetachContent()
        {
            provider.Clear();
            RemoveLogicalChild(Content);
        }

        public Grid Root
        {
            get
            {
                return this.LayoutRoot;
            }
        }
        #endregion

        public void SetTheme()
        {
            dataGrid1.FontFamily = new FontFamily("Tahoma");
        }
  
        ErrorProvider provider = new ErrorProvider();
        public MaskEditorDemo()
        {   
            InitializeComponent();
            PrepareGridData();
            dataGrid1.ItemsSource = listRecords;
            this.LayoutRoot.Children.Add(provider);
            dataGrid1.ColumnsHierarchy.AllowResize = false;
            // Subscribes to the CellValidating event.
            // You can use this event to cancel the EndEdit operation if the new cell value is not valid.
            dataGrid1.CellValidating += new VIBlend.WPF.Controls.DataGrid.CellCancelEventHandler(dataGrid1_CellValidating);
            this.dataGrid1.ColumnsHierarchy.Items[this.dataGrid1.ColumnsHierarchy.Items.Count - 1].CellEditorDeActivationFlags = EditorActivationFlags.KEY_PRESS_ENTER | EditorActivationFlags.MOUSE_CLICK;
            this.dataGrid1.ColumnsHierarchy.Items[this.dataGrid1.ColumnsHierarchy.Items.Count - 1].CellEditorActivationFlags = EditorActivationFlags.MOUSE_CLICK_SELECTED_CELL | EditorActivationFlags.KEY_PRESS_ENTER;
        }

        void dataGrid1_CellValidating(object sender, CellCancelEventArgs args)
        {
            provider.Clear();
            if (args.Cell.ColumnItem.Text.Equals("Phone Number"))
            {
                string value = (string)(args.Cell.EditorElement as MaskEditor).EditorValue;
                if (value.Length < 10)
                {
                    provider.BlinkStyle = ErrorProviderBlinkStyle.BlinkIfDifferentError;
                    provider.IconAlignment = ErrorProviderIconAlignment.MiddleLeft;
                    provider.SetError(args.Cell.EditorElement, "You must enter a valid phone number.");
                    args.Cancel = true;
                }
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            this.listRecords.Clear();
            this.listRecords = null;
            base.OnClosing(e);
            this.dataGrid1.CellValidating -= new VIBlend.WPF.Controls.DataGrid.CellCancelEventHandler(dataGrid1_CellValidating);
            this.dataGrid1.Dispose();
            this.dataGrid1 = null;
        }
        #region Sample Data

        #region Data
        string[] firstNames = new string[] 
                {
                    "Andrew",
                    "Nancy",
                    "Shelley",
                    "Regina",
                    "Yoshi",
                    "Antoni",
                    "Mayumi",
                    "Ian",
                    "Peter",
                    "Lars",
                    "Petra",
                    "Martin",
                    "Sven",
                    "Elio",
                    "Beate",
                    "Cheryl",
                    "Michael",
                    "Guylène"
                };

        string[] lastNames = new string[] 
                {
                    "Fuller",
                    "Davolio",
                    "Burke",
                    "Murphy",
                    "Nagase",
                    "Saavedra",
                    "Ohno",
                    "Devling",
                    "Wilson",
                    "Peterson",
                    "Winkler",
                    "Bein",
                    "Petersen",
                    "Rossi",
                    "Vileid",
                    "Saylor",
                    "Björn",
                    "Nodier"
                };

          string[] phones = new string[] 
                {
                    "(617)245-9601",
                    "(615)621-2121",
                    "(612)555-3251",
                    "(217)665-6231",
                    "(111)235-2313",
                    "(912)125-5443",
                    "(715)465-6789",
                    "(416)915-1256",
                    "(212)895-8382",
                    "(511)125-9191",
                    "(419)825-1362",
                    "(318)765-5566"
                };

         string[] addresses = new string[] 
                {
                    "512 EASY ST",
                    "2301 EASY AVE",
                    "5123 CENTER ST",
                    "BOX 74 RR1",
                    "BOX 512 RR5",
                    "1818 H Street, NW",
                    "2341 M Street, NW",
                    "705 19th Street, NW",
                    "1923 Pennsylvania Ave NW ",
                    "815 Second Ave",
                    "700 65th Street NY",
                    "231 Murray Drive, SW"
                };

        public class EmployeeSaleRecord
        {
            public EmployeeSaleRecord(string fName, string lName, string phoneNumber, string address)
            {
                this.fName = fName;
                this.lName = lName;
                this.address = address;
                this.phoneNumber = phoneNumber;
            }

            #region Private Members
            private string fName;
            private string lName;
            private string phoneNumber;
            private string address;
            #endregion

            #region Properties

            public string FirstName
            {
                get { return fName; }
                set { fName = value; }
            }

            public string LastName
            {
                get { return lName; }
                set { lName = value; }
            }

            public string Address
            {
                get { return address; }
                set { address = value; }
            }

            public string PhoneNumber
            {
                get { return phoneNumber; }
                set { phoneNumber = value; }
            }


            #endregion
        }

       #endregion

        public List<EmployeeSaleRecord> listRecords = new List<EmployeeSaleRecord>();

        private void PrepareGridData()
        {
            const int rowsToLoad = 100;
            listRecords.Clear();

            Random rand = new Random();

            for (int i = 0; i < rowsToLoad; i++)
            {
               listRecords.Add(new EmployeeSaleRecord(
                    firstNames[rand.Next(0, firstNames.Length - 1)],
                    lastNames[rand.Next(0, lastNames.Length - 1)],
                    phones[rand.Next(0, phones.Length - 1)],
                    addresses[rand.Next(0, addresses.Length - 1)])
                    );
            }
        }

        #endregion
    }

    public class PhoneNumberConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string valueString = value.ToString();
            string phoneNumberString = "";
            for (int i = 0; i < valueString.Length; i++)
            {
                if (Char.IsDigit(valueString[i]))
                {
                    phoneNumberString += valueString[i];
                }
            }

            return phoneNumberString;
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                string phoneString = value.ToString();

                if (phoneString.Length > 0)
                {
                    phoneString = phoneString.Insert(0, "(");
                }

                if (phoneString.Length > 4)
                {
                    phoneString = phoneString.Insert(4, ")");
                }

                if (phoneString.Length > 8)
                {
                    phoneString = phoneString.Insert(8, "-");
                }

                return phoneString;
            }
            else
            {
                return "";
            }
        }
    }
  
 }
